package Controls
{
	import Objects.GameBitmap;
	import Objects.GameSprite;
	
	import Plugin.TogglePlugin;
	
	import Setup.ZOrder;
	
	import Tools.DisplayTool;

	public class ComboBox extends GameSprite
	{
		public var list:GameSprite;
		public var renderList:Array;
		public var dropButton:Button;
		public var renderClass:Class;
		public var back:GameBitmap;
		public var toggle:TogglePlugin;
		public function ComboBox()
		{
			super();
			
			dropButton=new Button();
			addChild(dropButton);
			
			list=new GameSprite();
			gs.addGameObject(list,ZOrder.TIPS);
			
			back=new GameBitmap();
			list.addChild(back);
			
			intelligent=true;
		}
		override protected function $update():void
		{
			if(list.obj.visible)
			{
				if(gs.mHit&&!list.mOver)
				{
					hideList();
				}
			}
			if(dropButton.mHit)
			{
				showList();
			}
		}
		protected function showList():void
		{
			list.obj.visible=true;
		}
		protected function hideList():void
		{
			list.obj.visible=false;
		}
		protected final function selectHandler(index:int):void
		{
			hideList();
			var itemRender:ItemRender=renderList[index];
			handleSelect(itemRender.data);
		}
		protected function handleSelect(data:Object):void
		{
			
		}
		public final function setup(dataList:Array):void
		{
			DisplayTool.removeObjects(renderList);
			
			renderList=DisplayTool.createItemRenders(dataList,renderClass,list);
			
			hideList();
			
			if(toggle)
			{
				toggle.remove();
			}
			
			toggle=gs.addToggle(renderList,selectHandler);
			
			layout();
			
			_setup(dataList);
		}
		protected function layout():void
		{
			
		}
		protected function _setup(dataList:Array):void
		{
			
		}
		override protected function $remove():void
		{
			list.remove();
		}
	}
}